package action;

import config.ApplicationConfig;
import net.sf.json.JSONObject;
import server.LoginServer;
import server.SettingServer;
import server.UserServer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-20 Saturday 22:02:31
 */
public class MutableServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                String active = request.getParameter("active");
                if (active != null) {

                    UserServer userServer = new UserServer();
                    userServer.open();

                    int canChange = userServer.canChange(username, "");

                    if (canChange == 2) {
                        SettingServer settingServer = new SettingServer();
                        settingServer.open();
                        JSONObject temp = settingServer.readSettingObject("team");
                        if (temp.containsKey("mutable")) {
                            temp.replace("mutable", !temp.getBoolean("mutable"));
                        } else {
                            temp.put("mutable", true);
                        }
                        settingServer.recordSettingObject("team", temp);
                        settingServer.close();
                        session.setAttribute("infoContent", "更改成功");
                        session.setAttribute("panelColor", "blue");
                    } else {
                        session.setAttribute("infoContent", "更改失败，你没有权限修改");
                        session.setAttribute("panelColor", "red");
                    }
                    userServer.close();
                    session.setAttribute("infoTitle", "更改可修改队名状态");
                    session.setAttribute("return", "mine");
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
